package com.yssoft.controller.app.inventory;

import com.google.common.collect.Lists;
import com.yssoft.entity.inventory.TOutstock;
import com.yssoft.entity.inventory.TOutstockApplyDetail;
import com.yssoft.entity.production.TLabels;
import com.yssoft.entity.production.TPackageCode;
import com.yssoft.service.inventory.OutstockService;
import com.yssoft.utils.AppUtil;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

import java.lang.reflect.Field;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @类名称: OutstockController
 * @类描述: 出库管理控制器
 * @作者: 洪丰
 * @创建时间:2018年5月17日 下午2:30:47
 */
@CrossOrigin//解决跨域session问题
@Controller
@RequestMapping("/app/outstock")
public class OutstockController {
    
    @Resource
    private OutstockService outstockService;
    
    /**
     * @方法名称: getOutstockApplyList
     * @功能描述: 获取出库申请单列表
     * @作者: 洪丰
     * @创建时间:2018年5月17日 下午3:56:44
     * @param page
     * @param limit
     * @param companyId
     * @param source
     * @param state
     * @param deliveryDate
     * @return Object
     */
    @RequestMapping(value = "getOutstockApplyList",method = RequestMethod.GET)
    @ResponseBody
    public Object getOutstockApplyList(Integer page, Integer limit, Integer companyId, String source, String state, String deliveryDate) {
    	if (null == companyId) {
            return AppUtil.returnMapJson("0", "查询失败，请传递企业id", null);
        } 
        try {
            List<Map<String, Object>> outstockApplyList = outstockService.findOutstockApplyPage(page, limit, companyId, source, state, deliveryDate);
            return AppUtil.returnListJson("1", "查询成功!", outstockApplyList);
        } catch (NumberFormatException e) {
        	e.printStackTrace();
            return AppUtil.returnMapJson("0", "查询失败!"+e.getMessage(), null);
        }
    }
    
    /**
     * @方法名称: getOutstockList
     * @功能描述: 获取出库单列表
     * @作者: 洪丰
     * @创建时间:2018年5月17日 下午3:57:58
     * @param page
     * @param limit
     * @param companyId
     * @param source
     * @param state
     * @param deliveryDate
     * @return Object
     */
    @RequestMapping(value = "getOutstockList",method = RequestMethod.GET)
    @ResponseBody
    public Object getOutstockList(Integer page, Integer limit, Integer companyId, String source, String state, String deliveryDate) {
    	if (null == companyId) {
            return AppUtil.returnMapJson("0", "查询失败，请传递企业id", null);
        } 
        try {
            List<Map<String, Object>> outstockApplyList = outstockService.findOutstockPage(page, limit, companyId, source, state, deliveryDate);
            return AppUtil.returnListJson("1", "查询成功!", outstockApplyList);
        } catch (NumberFormatException e) {
        	e.printStackTrace();
            return AppUtil.returnMapJson("0", "查询失败!"+e.getMessage(), null);
        }
    }
    
    /**
     * @方法名称: getOutstockApplyDetail
     * @功能描述: 获取出库申请明细
     * @作者: 洪丰
     * @创建时间:2018年5月17日 下午4:31:53
     * @param outstockApplyId
     * @return Object
     */
    @RequestMapping(value = "getOutstockApplyDetail",method = RequestMethod.GET)
    @ResponseBody
    public Object getOutstockApplyDetail(Integer outstockApplyId) {
    	if (null == outstockApplyId) {
            return AppUtil.returnMapJson("0", "查询失败，请传递出库申请单id", null);
        } 
        try {
            Map<String, Object> outstockApply = outstockService.findOneOutstockApply(outstockApplyId);
            return AppUtil.returnMapJson("1", "查询成功!", outstockApply);
        } catch (NumberFormatException e) {
        	e.printStackTrace();
            return AppUtil.returnMapJson("0", "查询失败!"+e.getMessage(), null);
        }
    }
    
    /**
     * @方法名称: getInstockDetailMOrSList
     * @功能描述: 获取配货入库详细列表（物料或半成品）
     * @作者: 洪丰
     * @创建时间:2018年5月17日 下午7:29:11
     * @param outAppDetailId
     * @param pType
     * @param inStockData
     * @return Object
     */
    @RequestMapping(value = "getInstockDetailMOrSList",method = RequestMethod.GET)
    @ResponseBody
    public Object getInstockDetailMOrSList(Integer outAppDetailId, String pType, String inStockData) {
    	if (null == outAppDetailId) {
            return AppUtil.returnMapJson("0", "查询失败，请传递出库申请单明细id", null);
        } 
        try {
            List<Map<String, Object>> instockList = Lists.newArrayList();
            if("3".equals(pType)){
            	instockList = outstockService.findInstockByMaterialId(outAppDetailId, inStockData);
            }else if("2".equals(pType)){
            	instockList = outstockService.findInstockBySfgId(outAppDetailId, inStockData);
            }
            return AppUtil.returnListJson("1", "查询成功!", instockList);
        } catch (NumberFormatException e) {
        	e.printStackTrace();
            return AppUtil.returnMapJson("0", "查询失败!"+e.getMessage(), null);
        }
    }
    
    /**
     * @方法名称: checkCodePBList
     * @功能描述: 商品批次溯源出库校验标签码
     * @作者: 洪丰
     * @创建时间:2018年5月18日 上午9:02:30
     * @param outAppDetailId
     * @param inStockData
     * @param companyId
     * @param labelCode
     * @return Object
     */
    @RequestMapping(value = "checkCodePBList",method = RequestMethod.GET)
    @ResponseBody
    public Object checkCodePBList(Integer outAppDetailId, String inStockData, Integer companyId, String labelCode) {
    	if (null == outAppDetailId) {
            return AppUtil.returnMapJson("0", "扫码失败，请传递出库申请单明细id", null);
        }
    	if (null == companyId) {
            return AppUtil.returnMapJson("0", "扫码失败，请传递企业id", null);
        }
        try {
        	TLabels lEntity = outstockService.checkBatchCodeExist(labelCode, companyId, "2");
        	if(null == lEntity || null == lEntity.gettWorksheeId()){
        		return AppUtil.returnListJson("0", "扫码失败，无效批次码!", null);
        	}
        	Integer proId = outstockService.checkProExist(lEntity.gettWorksheeId());
        	if(null == proId){
        		return AppUtil.returnListJson("0", "扫码失败，无效批次码!", null);
        	}
        	TOutstockApplyDetail oadEntiy = outstockService.findOneDetail(outAppDetailId);
        	if(oadEntiy.gettPId().compareTo(proId) != 0){
        		return AppUtil.returnListJson("0", "扫码失败，该码不属于出库产品!", null);
        	}
        	Map<String, Object> resultMap = new HashMap<String, Object>();
        	resultMap.put("worksheeId", lEntity.gettWorksheeId());
        	return AppUtil.returnMapJson("1", "扫码成功!", resultMap);
        } catch (NumberFormatException e) {
        	e.printStackTrace();
            return AppUtil.returnMapJson("0", "扫码失败!"+e.getMessage(), null);
        }
    }
    
    /**
     * @方法名称: getInstockDetailPLList
     * @功能描述: 个体溯源扫码出库获取入库单明细
     * @作者: 洪丰
     * @创建时间:2018年5月18日 上午9:57:54
     * @param outAppDetailId
     * @param companyId
     * @param labelCode
     * @return Object
     */
    @RequestMapping(value = "getInstockDetailPLList",method = RequestMethod.GET)
    @ResponseBody
    public Object getInstockDetailPLList(Integer outAppDetailId, Integer companyId, String labelCode) {
    	if (null == outAppDetailId) {
            return AppUtil.returnMapJson("0", "扫码失败，请传递出库申请单明细id", null);
        }
    	if (null == companyId) {
            return AppUtil.returnMapJson("0", "扫码失败，请传递企业id", null);
        }
        try {
        	TLabels lEntity = outstockService.checkBatchCodeExist(labelCode, companyId, "1");
        	TPackageCode pcEntity = outstockService.checkPaceageCodeExist(labelCode, companyId);
        	if(null == lEntity && null == pcEntity){
        		return AppUtil.returnListJson("0", "扫码失败，无效个体码!", null);
        	}
        	Integer proId = null;
        	if(lEntity != null){
        		if(null == lEntity.gettWorksheeId()){
        			return AppUtil.returnListJson("0", "扫码失败，无效个体码!", null);
        		}
        		if(null != lEntity.gettPackageId() ){
        			return AppUtil.returnMapJson("0", "扫码失败!请扫描包装码", null);
        		}
        		proId = outstockService.checkProExist(lEntity.gettWorksheeId());
        	}
        	if(pcEntity != null){
        		if(null == pcEntity.gettWorksheeId()){
        			return AppUtil.returnListJson("0", "扫码失败，无效个体码!", null);
        		}
        		proId = outstockService.checkProExist(pcEntity.gettWorksheeId());
        	}
        	if(null == proId){
        		return AppUtil.returnListJson("0", "扫码失败，无效批次码!", null);
        	}
        	TOutstockApplyDetail oadEntiy = outstockService.findOneDetail(outAppDetailId);
        	if(oadEntiy.gettPId().compareTo(proId) != 0){
        		return AppUtil.returnListJson("0", "扫码失败，该码不属于出库产品!", null);
        	}
        	
        	Map<String, Object> resultMap = new HashMap<String, Object>();
        	if(lEntity != null){
        		Map<String, Object> inDeltalMap = outstockService.selectinDetailByLabelId(lEntity.getId());
        		resultMap.put("labelsMap", inDeltalMap);
        	}
        	if(pcEntity != null){
        		List<Map<String, Object>> inDeltalList = outstockService.selectinDetailByPaceageId(pcEntity.getId());
        		Class className = pcEntity.getClass();
                Map<String,Object> param=new HashMap<>();
                for(; className != Object.class ; className = className.getSuperclass()) {
                    Field[] fields = className.getDeclaredFields();//获取所有私有字段
                    for (Field field : fields) {
                        field.setAccessible(true);
                        param.put(field.getName(), field.get(pcEntity)==null?"":field.get(pcEntity));
                    }
                }
        		resultMap.put("paceageCodeMap", param);
        		resultMap.put("instockList", inDeltalList);
        	}
        	return AppUtil.returnMapJson("1", "扫码成功!", resultMap);
        } catch (NumberFormatException e) {
        	e.printStackTrace();
            return AppUtil.returnMapJson("0", "扫码失败!"+e.getMessage(), null);
        } catch (IllegalArgumentException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			return AppUtil.returnMapJson("0", "扫码失败!"+e.getMessage(), null);
		} catch (IllegalAccessException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			return AppUtil.returnMapJson("0", "扫码失败!"+e.getMessage(), null);
		}
    }
    
    /**
     * @方法名称: savePurchaseRequest
     * @功能描述: 保存出库信息
     * @作者: 洪丰
     * @创建时间:2018年5月19日 上午10:13:19
     * @param request
     * @param entity
     * @return Object
     */
    @RequestMapping(value = "saveOutstock",method = RequestMethod.POST)
    @ResponseBody
    public Object savePurchaseRequest(HttpServletRequest request,TOutstock entity) {
        try {
        	outstockService.save(request, entity);
        	return AppUtil.returnMapJson("1", "保存成功!", null);
        } catch (NumberFormatException e) {
        	e.printStackTrace();
            return AppUtil.returnMapJson("0", "保存失败!"+e.getMessage(), null);
        }
    }
    
    /**
     * @方法名称: getInstockDetailPBList
     * @功能描述: 批次扫码配货，获取入库明细
     * @作者: 洪丰
     * @创建时间:2018年5月21日 上午8:47:05
     * @param worksheeId
     * @param inStockData
     * @return Object
     */
    @RequestMapping(value = "getInstockDetailPBList",method = RequestMethod.GET)
    @ResponseBody
    public Object getInstockDetailPBList(Integer worksheeId, String inStockData) {
    	if (null == worksheeId) {
            return AppUtil.returnMapJson("0", "查询失败，请传递加工单明细id", null);
        }
        try {
        	List<Map<String, Object>> instockList = outstockService.findInstockByWorksheeId(worksheeId, inStockData);
        	return AppUtil.returnListJson("1", "查询成功!", instockList);
        } catch (NumberFormatException e) {
        	e.printStackTrace();
            return AppUtil.returnMapJson("0", "查询失败!"+e.getMessage(), null);
        }
    }
    
    /**
     * @方法名称: getLabelsList
     * @功能描述: 获取个体溯源出库明细对应的标签码列表
     * @作者: 洪丰
     * @创建时间:2018年5月22日 下午7:41:04
     * @param outDetailId
     * @return Object
     */
    @RequestMapping(value = "getLabelsList",method = RequestMethod.GET)
    @ResponseBody
    public Object getLabelsList(Integer outDetailId) {
    	if (null == outDetailId) {
            return AppUtil.returnMapJson("0", "查询失败，请传递出库明细id", null);
        }
        try {
        	List<Map<String, Object>> instockList = outstockService.findInstockAndLablesList(outDetailId);
        	return AppUtil.returnListJson("1", "查询成功!", instockList);
        } catch (NumberFormatException e) {
        	e.printStackTrace();
            return AppUtil.returnMapJson("0", "查询失败!"+e.getMessage(), null);
        }
    }
}
